import { defineConfig } from "vite";
import legacy from "@vitejs/plugin-legacy";
import react from "@vitejs/plugin-react";
import { resolve } from "node:path";
import { version } from "./package.json";

// https://vite.dev/config/
export default defineConfig({
  base: "./",
  css: {
    modules: {
      localsConvention: "camelCaseOnly",
    },
    preprocessorOptions: {
      less: {
        javascriptEnabled: true,
      },
    },
  },
  // 获取当前package版本好
  define: {
    __APP_VERSION__: `"${version}"`,
  },
  plugins: [
    react(),
    // 兼容一下es的高级语法
    legacy({
      targets: ["defaults", "ie >= 11", "chrome 52"], //需要兼容的目标列表，可以设置多个
      additionalLegacyPolyfills: ["regenerator-runtime/runtime"], // 引入额外的旧版 polyfill，以兼容更旧的语法（例如 async/await）。
      renderLegacyChunks: true, // 开启后，生成用于旧浏览器的构建代码（chunk）。
      polyfills: [
        "es.symbol",
        "es.array.filter",
        "es.promise",
        "es.promise.finally",
        "es/map",
        "es/set",
        "es.array.for-each",
        "es.object.define-properties",
        "es.object.define-property",
        "es.object.get-own-property-descriptor",
        "es.object.get-own-property-descriptors",
        "es.object.keys",
        "es.object.to-string",
        "web.dom-collections.for-each",
        "esnext.global-this",
        "esnext.string.match-all",
      ],
    }),
  ],
  resolve: {
    alias: [
      {
        find: "@",
        replacement: resolve(__dirname, "./src"),
      },
    ],
  },
  server: {
    host: true,
  },
});
